package cn.ssm.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.ssm.biz.impl.BanZhuRenBizImpl;
import cn.ssm.biz.impl.ClassroomBizImpl;
import cn.ssm.biz.impl.ComputerroomBizImpl;
import cn.ssm.biz.impl.TeacherBizImpl;
import cn.ssm.pojo.BanZhuRen;
import cn.ssm.pojo.Class;
import cn.ssm.pojo.Computerroom;
import cn.ssm.pojo.Pager;
import cn.ssm.pojo.Teacher;

@Controller
@RequestMapping("/classroom")
public class ClassroomController {
	@Autowired
	@Qualifier("computerroomBizImpl")
	private ComputerroomBizImpl com;
	public ComputerroomBizImpl getCom() {
		return com;
	}
	public void setCom(ComputerroomBizImpl com) {
		this.com = com;
	}

	
	
	
	public BanZhuRenBizImpl getBanZhuRenBizImpl() {
		return BanZhuRenBizImpl;
	}
	public void setBanZhuRenBizImpl(BanZhuRenBizImpl banZhuRenBizImpl) {
		BanZhuRenBizImpl = banZhuRenBizImpl;
	}
	public TeacherBizImpl getTea() {
		return tea;
	}
	public void setTea(TeacherBizImpl tea) {
		this.tea = tea;
	}
	@Autowired
	@Qualifier("BanZhuRenBizImpl")
	private BanZhuRenBizImpl BanZhuRenBizImpl;
	@Autowired
	@Qualifier("TeacherBizImpl")
	private TeacherBizImpl tea;
	@Autowired
	@Qualifier("ClassroomBizImpl")
	private ClassroomBizImpl classroomBizImpl;
	public ClassroomBizImpl getClassroomBizImpl() {
		return classroomBizImpl;
	}
	public void setClassroomBizImpl(ClassroomBizImpl classroomBizImpl) {
		this.classroomBizImpl = classroomBizImpl;
	}

	@RequestMapping("/addClassroom")
	public String addClassroom(ModelMap mo,@RequestParam String className,@RequestParam Integer computerroomId,@RequestParam Integer teacherId,@RequestParam Integer banZhuRenId){
		
		

		Class clr = new Class();
		clr.setClassName(className);
		clr.setComputerroomId(computerroomId);
		clr.setTeacherId(teacherId);
		clr.setBanZhuRenId(banZhuRenId);
		
		
		
		int num = classroomBizImpl.insertClass(clr);
		String url="";
		if(num>0){
			url="redirect:/classroom/findClassroomInfo";
		}else{
			url="classAdd";
		}
		return url;
	}
	
	
	@RequestMapping("/findClassroomOne")
	public String findClassroomOne(ModelMap mo){
		
	
		List<Teacher> teacher=tea.getTeacherList1();
		List<BanZhuRen> banZhuRen=BanZhuRenBizImpl.getBanzhurenInfo();
		List<Computerroom> computerroom=com.getRoomdescribe();
		System.out.println("roomlise"+computerroom.size());
		mo.put("teacher",teacher);
		mo.put("banZhuRen",banZhuRen);
		mo.put("computerroom",computerroom);
		return "classAdd";
	}
	
	
	
	@RequestMapping("/delClassroom")
	public String delClassroom(int id){
		int num = classroomBizImpl.delClass(id);
		System.out.println("del");
		String url="";
		if(num>0){
			url="redirect:/classroom/findClassroomInfo";
		}else{
			url="404";
		}
		return url;
	}
	@RequestMapping("/updateClassroom")
	public String updateClassroom(Class clr,@RequestParam("id") int id,@RequestParam String className,@RequestParam("computerroomId") int computerroomId,@RequestParam("teacherId") int teacherId,@RequestParam("banZhuRenId") int banZhuRenId){
//		int computerroomId =com.findIdByName(computerroomName);
//		int teacherId =tea.findIdByName(teacherName);
		clr.setClassName(className);
		clr.setComputerroomId(computerroomId);
		clr.setTeacherId(teacherId);
		clr.setBanZhuRenId(banZhuRenId);
		clr.setId(id);
		int num = classroomBizImpl.updateClass(clr);
		
		String url="";
		if(num>0){
			url="redirect:/classroom/findClassroomInfo";
		}else{
			url="404";
		}
		return url;
	}
	
	
	
	
	
	@RequestMapping("/findClassroomInfo")
	public String findClassroomInfo(Model model,HttpServletRequest request){
		List<Class> classroomInfo = classroomBizImpl.classroomInfo1();
		System.out.println("==================="+classroomInfo.size());
		model.addAttribute("classroomInfo", classroomInfo);
		String currentPageStr =request.getParameter("currentPage");
		//System.out.println(currentPageStr);
		int rowCount = classroomBizImpl.findClassCount();
		System.out.println("num:"+rowCount);
		Pager pager=null;
		if(currentPageStr==null){
			pager=new Pager(rowCount,3,1);
		}else{
			int currentPage=Integer.parseInt(request.getParameter("currentPage"));
			pager=new Pager(rowCount, 3, currentPage);
		}
		pager.setUrl("/classroom/findClassroomInfo");
		model.addAttribute("pager", pager);
		
		List<Class> cList= classroomBizImpl.classroomInfo(pager);
		String url="";
		if(cList!=null){
			model.addAttribute("cList", cList);
			url="classList";
			System.out.println("cList"+cList.size());
		}else{
			url="404";
		}
		return url;
	}
	
	
	
	
	
	@RequestMapping("/update")
	public String update(int id,ModelMap m){
		
		Class cla= classroomBizImpl.selectByID(id);
		System.out.println("============教员"+cla.getTeacherId());
		List<Teacher> teacher=tea.getTeacherList1();
		List<Computerroom> computerroom=com.getRoomdescribe();
		List<BanZhuRen> banZhuRen=BanZhuRenBizImpl.getBanzhurenInfo();
		m.put("teacher",teacher);
		m.put("computerroom",computerroom);
		m.put("banZhuRen", banZhuRen);
		String url="";
		if(cla!=null){
			m.addAttribute("cla", cla);
			url="updateClass";
		}else{
			url="404";
		}
		return url;
	}
}
